class Solution {
public:
    int trap(vector<int>& height) {
        int len=height.size();
        vector<int> left(len,0);
        vector<int> right(len,0);
        left[0]=height[0];
        right[len-1]=height[len-1];
        for(int i=1;i<len;i++){
            left[i]=max(left[i-1],height[i]);
        }
        for(int i=len-2;i>=0;i--){
            right[i]=max(right[i+1],height[i]);
        }
        int ans=0;
        for(int i=1;i<len-1;i++){
            ans+=min(left[i],right[i])-height[i];
        }
        return ans;
    }
};